package com.calclab.hablar.rooms.client.settings;

import com.calclab.hablar.core.client.validators.HasState;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.LabelElement;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyDownHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class SetRoomWidget extends Composite implements SetRoomDisplay{

    private static SetRoomWidgetUiBinder uiBinder = GWT.create(SetRoomWidgetUiBinder.class);

    interface SetRoomWidgetUiBinder extends UiBinder<Widget, SetRoomWidget> {
    }

    @UiField
    Button confirm, cancel;
    @UiField
    FlowPanel mediasList;
    @UiField
    TextBox description, roomName;
    @UiField
    LabelElement mediasLabel;
    @UiField
    Label roomNameError;

    @UiField
    SpanElement title;
    
    public SetRoomWidget() {
	initWidget(uiBinder.createAndBindUi(this));

	confirm.ensureDebugId("SetRoomWidget-confirm");
	cancel.ensureDebugId("SetRoomWidget-cancel");
	mediasList.ensureDebugId("SetRoomWidget-list");
    }

    @Override
    public void addItem(final SelectMediaItemDisplay itemDisplay) {
	mediasList.add(itemDisplay.asWidget());
    }

    @Override
    public Widget asWidget() {
	return this;
    }

    @Override
    public void clearList() {
	mediasList.clear();
    }

    @Override
    public SelectMediaItemDisplay createItem() {
	return new SelectMediaItemWidget();
    }

    @Override
    public HasClickHandlers getCancel() {
	return cancel;
    }

    @Override
    public HasClickHandlers getConfirm() {
	return confirm;
    }

    @Override
    public HasText getDescription() {
	return description;
    }

    @Override
    public HasText getRoomName() {
	return roomName;
    }

    @Override
    public HasText getRoomNameError() {
	return roomNameError;
    }
    
    @Override
    public void setPageTitle(String text){
	title.setInnerText(text);
    }

    @Override
    public void setRoomNameEnabled(boolean enabled){
	roomName.setEnabled(enabled);
    }
    
    @Override
    public HasKeyDownHandlers getRoomNameKeys() {
	return roomName;
    }
    
    @Override
    public HasState<Boolean> getConfirmEnabled(){
	return new HasState<Boolean>() {
	    @Override
	    public void setState(final Boolean state) {
		confirm.setEnabled(state);
	    }
	};
    }
}
